<?php

/**
 *
 * Copyright (c) 2011 Tomáš Holub (tomas.holub@webadvice.cz)
 * 
 * @author Tomáš Holub (tomas.holub@webadvice.cz)
 * @package OverFishCMS
 */

namespace AdminModule\Forms;

use Nette\Application\UI\Form;

class MenuForm extends Form {
    
    public function __construct($parent, $name)
    {
        parent::__construct($parent, $name);

        $this->addHidden("id");
        $this->addText('title', 'Titulek:')
                ->setRequired('Titulek musí být vyplněn')
                ->setAttribute("class",  "inp-form");
        $this->addSubmit('send', 'Odeslat');
        $this->onSuccess[] = array($this, 'formSubmited');
    }
    
    public function formSubmited($form)
    {
        if ($form->isValid()) {

            $values = $form->getValues();

            if (!$values->id){

                $menu = $this->getPresenter()->entities->menu;
                
                $menu->setTitle($values->title);
                
                $this->getPresenter()->repositories->menu->save($menu);
                
                $this->getPresenter()->flashMessage('Menu bylo vytvořeno.', 'success');
                
                $this->getPresenter()->redirect(':Admin:Menu:default');
            } else {
                
                $menu = $this->getPresenter()->repositories->menu->find($values->id);
    
                $menu->setTitle($values->title);
                
                $this->getPresenter()->repositories->menu->save($menu);
                
                $this->getPresenter()->flashMessage('Menu bylo uloženo.', 'success');
                
                $this->getPresenter()->redirect(':Admin:Menu:default');
            }
            
            
            
        }
    }
}

?>
